/****************************************************************************
 * 2024.9 ADMIN-20230317Z
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace QFramework.Monk
{
	public partial class CoinUpPanel : UIElement,IController
	{
		private void Awake()
		{

			var coinUpSystem = this.GetSystem<CoinUpSystem>();
			foreach (var coinUpItem in coinUpSystem.coinUpItems)
			{
				CoinUpTemplate.InstantiateWithParent(Group).Self((button =>
				{
                    button.onClick.AddListener((() =>
					{
						AudioKit.PlaySound("ability_level_up");
						coinUpItem.OnUpgrade();
						
					}));
					
					button.GetComponentInChildren<Text>().text =
						string.Format("{0}:{1}金币", coinUpItem.mAbilityConfig.Desc, coinUpItem.mAbilityConfig.Price);
					if (coinUpItem.CheckCanUpgrade())
					{
						button.interactable = true;
					}
					else
					{
						button.interactable = false;
					}
					
					coinUpItem.onChanged.Register(() =>
					{
						button.GetComponentInChildren<Text>().text =
							string.Format("{0}:{1}金币", coinUpItem.mAbilityConfig.Desc,
								coinUpItem.mAbilityConfig.Price);
						if (coinUpItem.CheckCanUpgrade())
						{
							button.interactable = true;
						}
						else
						{
							button.interactable = false;
						}
						
					}).UnRegisterWhenGameObjectDestroyed(button.gameObject);

					AppStart.goldNum.Register((num) =>
					{
						if (coinUpItem.CheckCanUpgrade())
						{
							button.interactable = true;
						}
						else
						{
							button.interactable = false;
						}
					}).UnRegisterWhenGameObjectDestroyed(button.gameObject);

				})).Show();
			}
			
			GoBack.onClick.AddListener(() =>
			{
				this.Hide();
			});
		}

		protected override void OnBeforeDestroy()
		{
		}

		public IArchitecture GetArchitecture()
		{
			return AppStart.Interface;
		}
	}
}